﻿//配置插件目录
layui.config({
    base: '/mods/'
});
layui.use(['form', 'laydate', 'util', 'jquery', 'layer', 'table','tree'], function () {
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var util = layui.util;
    var tid = null;
    var fid = null;
    var $ = layui.jquery;
    var tree = layui.tree;
    $(function () {
        ShowInfo();
        setTimeout(1);
    });
    $('#onAddinfo').on('click', function () {
        // 批量事件
        layer.open({
            type: 1, // page 层类型
            area: ['1200px', '800px'],
            title: '新增事件类型',
            shade: 0,//控制遮罩
            shadeClose: true, // 点击遮罩区域，关闭弹层
            maxmin: true, // 允许全屏最小化
            anim: 0, // 0-6 的动画形式，-1 不开启
            content: $("#add-main"),
        });
    });
    // 单删除
    function deleteData(id) {
        $.ajax({
            url: `http://8.140.245.229:8000/webroute/api/Event/HandleDelEvent?Id=${id}`,
            type: 'delete',
            success: function (res) {
                if (res.data != 0) {
                    layer.msg('删除成功');
                    ShowInfo(1);
                } else {
                    layer.msg('删除失败');
                }
            },
            error: function () {
                layer.msg('删除请求失败');
            }
        });
    }
    // 监听工具条，实现删除功能
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.EventId); // 传递正确的数据项ID
                layer.close(index);
            });
        } else if (obj.event == 'edit') {
            location.href = `/Event/EventEdit?id=${data.EventId}`;
        }
    });
    //渲染
    laydate.render({
        elem: '#storageDate'
    });
    function ShowInfo() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleShowAdmin",
            type: 'get',
            dataType: 'json',
            data: {
                TypeId: $("#DocumentNumber").val(),
                TypeName: $("#DocumentName").val()
            },
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                table.render({
                    elem: '#test',
                    cols: [[
                        { type: 'checkbox', align: 'center' },
                        { field: 'TypeId', title: '类型编号', align: 'center' },
                        { field: 'PId', title: '父类型', align: 'center' },
                        { field: 'TypeName', title: '类型名称', align: 'center' },
                        { title: '操作', width: 150, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true }
                    ]],
                    data: res
                })
                $.ajax({
                    url: 'http://8.140.245.229:8000/routechen/api/ReadEvent/HandleEventTree',
                    data: {
                        PId: 0,
                    },
                    type: 'get',
                    headers: {
                        'Authorization': "Bearer " + localStorage["token"] + ""
                    },
                    success: function (data) {
                        tree.render({
                            //id: 'testInfo',·
                            elem: '#ID-tree-demo-showLine',  // 容器id
                            data: data,  // 后台返回的树形数据
                            showLine: false, // 是否开启连接线
                            click: function (obj) {
                                var data = obj.data;
                                tid = data.id;
                                //clickTree();
                                //checkTree();
                            }
                        });
                    }
                });
            }
        })
    }
    $("#searchBtn").on('click', function () {
        ShowInfo();
    })
})
